﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace CoolCode.Controls
{
    public class WindowPanel : ContentControl
    {
       private Popup _popup;
        internal static Canvas _defaultCanvas;
        internal Canvas _canvas;
        public Canvas Canvas
        {
            get
            {
                return this._canvas;
            }
        }

        static WindowPanel()
        {
            Init(); 
        }

        private static void Init()
        {
            if (_defaultCanvas == null)
            {
                _defaultCanvas = new Canvas();
                FrameworkElement root = Application.Current.RootVisual as FrameworkElement;
                Action setSize = delegate
                {
                    _defaultCanvas.Width = root.ActualWidth;
                    _defaultCanvas.Height = root.ActualHeight;
                };
                root.SizeChanged += (sender, e) =>
                {
                    setSize();
                };
                setSize();
            }
        }

        public WindowPanel()
            : base()
        {
            this.DefaultStyleKey = typeof(DialogPanel);
            Init();
            _canvas = _defaultCanvas;
            _popup = new Popup();
            _popup.Child = _canvas;
            Left = 0; Top = 0;
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            if (this.DragEnable)
            {
                UIElement header = this.GetTemplateChild("Header") as UIElement;
                UIElement root = this.GetTemplateChild("root") as UIElement;

                DragHelper dragHelper = new DragHelper();
                dragHelper.BindMoveFunction(header, root);
            }

            ButtonBase close = this.GetTemplateChild("close") as ButtonBase;
            close.Click += new RoutedEventHandler(close_Click);

            Storyboard storyboard = this.GetTemplateChild("storyboard") as Storyboard;
            storyboard.Begin();
        }

        void close_Click(object sender, RoutedEventArgs e)
        {
            if (this.OnClose != null)
                OnClose(sender, e);
        }

        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(DialogPanel), null);
        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        public RoutedEventHandler OnClose { get; set; }

        public bool DragEnable { get; set; }

        public double Left { get { return _popup.HorizontalOffset; } set { _popup.HorizontalOffset=value; } }

        public double Top { get { return _popup.VerticalOffset; } set { _popup.VerticalOffset  = value; } }

        public void Show()
        {
            //CenterOnScreen();
            this.Left = (this._canvas.ActualWidth - base.ActualWidth) / 2.0;
            this.Top = (this._canvas.ActualHeight - base.ActualHeight) / 2.0;
            if (!this._canvas.Children.Contains(this))
            {
                this._canvas.Children.Add(this);
            }
            base.Visibility = Visibility.Visible;
            _popup.IsOpen = true;

        }

        public void Close()
        {
            if (this._canvas.Children.Contains(this))
            {
                this._canvas.Children.Remove(this);
            } 
            _popup.IsOpen = false ;
        }

        public void Hide()
        {
            base.Visibility = Visibility.Collapsed;
        }

        internal void CanvasSizeChanged(object sender, SizeChangedEventArgs e)
        {
            //if (this.IsMaximized)
            //{
            //    base.Width = this.Canvas.ActualWidth;
            //    base.Height = this.Canvas.ActualHeight;
            //}
            //else
            //{
                if ((this.Left + base.ActualWidth) > this.Canvas.ActualWidth)
                {
                    this.Left = this.Canvas.ActualWidth - base.ActualWidth;
                }
                if ((this.Top + base.ActualHeight) > this.Canvas.ActualHeight)
                {
                    this.Top = this.Canvas.ActualHeight - base.ActualHeight;
                }
                if (this.Left < 0.0)
                {
                    this.Left = 0.0;
                }
                if (this.Top < 0.0)
                {
                    this.Top = 0.0;
                }
            //}
            //this._context.ModalLayer.Width = this.Canvas.ActualWidth;
            //this._context.ModalLayer.Height = this.Canvas.ActualHeight;
            this._canvas.Clip = new RectangleGeometry { Rect = new Rect(0.0, 0.0, this._canvas.ActualWidth, this._canvas.ActualHeight) };
           
        }

        public void CenterOnScreen()
        {
            this.OnLayout(delegate
            {
                this.Left = (this._canvas.ActualWidth - base.ActualWidth) / 2.0;
                this.Top = (this._canvas.ActualHeight - base.ActualHeight) / 2.0;
            });
        }

        private void OnLayout(Action a)
        {
            EventHandler handler = delegate(object s, EventArgs e)
            {
            };
            handler = delegate(object s, EventArgs e)
            {
                a();
                this.LayoutUpdated -= handler;
            };
            base.LayoutUpdated += handler;
            base.InvalidateMeasure();
        }





    }
}
